package com.toutiao.issue

import scala.io.Source
import java.io.PrintWriter

import scala.collection.mutable.{HashMap => HMap}

import net.liftweb.json.{JsonAST,Extraction,Printer,parse}
import net.liftweb.json.JsonDSL._

import com.toutiao.score.Scorer

case class RawDocument(
		val name:String,
		val title:String,
		val score:Map[String,Double])

object Data {
  
  implicit val _ = net.liftweb.json.DefaultFormats

  def getDocument(location:String) = {
    val source = Source.fromFile(location,"UTF-8")
    val lineIterator = source.getLines
    val t = SimpleDate.current
    val xs = lineIterator.map(line => {
      val x = parse(line).extract[RawDocument]
      val name = x.name
      val title = x.title.split(" ")
      val vs = x.score
      
      new Document(name,vs,title,t)
      
    })
        
    xs.toArray
  }
  
  def getRawDocument(location:String) = {
    val source = Source.fromFile(location,"UTF-8")
    val lineIterator = source.getLines
    val xs = lineIterator.map(line => {
      val x = parse(line).extract[RawDocument]
      x
    })
    
    xs.toArray
  }
  
  def convertDocument(scorer:Scorer, xs:Array[RawDocument]) = {
    val t = SimpleDate.current
    xs.map(x => {
      val name = x.name
      val title = x.title.split(" ")
      val vs = scorer.score(title)
      new Document(name,vs,title,t)
    })
  }
  
  def saveTopic(data:Map[String,Topic],location:String) = {
    val xs = data.toArray.sortBy(-_._2.ms.size)
    val out = new PrintWriter(location)
    for (x <- xs){
      val nm = x._1
      val ms = x._2.ms
      val vs = x._2.vs
      val z = ("NAME"->nm)~("VS"->vs)~("MS"->ms.toList)
      val line = Printer.compact(JsonAST.render(z))
      out.write(line+"\n")      
    }
    out.close
  }
  

}
